﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.PRM.AppCode.Lib;

namespace mojoPortal.Web.RS.PRM.Controls
{
    public partial class OwnerListControl : RSBaseUserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
        }
        protected void btnSearchOwner_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            string lastname = txtOwnerLastName.Text;
            string firstname = txtOwnerFirstName.Text;
            LoadOwners(userIdentity.RSCompanyId, firstname, lastname);
        }
        protected void btnAddOwner_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            ucModalOwnerControl.ShowModal(userIdentity.RSCompanyId, 0);
        }
        protected void gvOwner_Edit(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int ownerid = Convert.ToInt32(e.CommandArgument.ToString());
            ucModalOwnerControl.ShowModal(userIdentity.RSCompanyId, ownerid);
        }
        public bool LoadOwners(int companyid, string firstname="", string lastname="")
        {
            bool bHasValue = false;
            if (companyid > 0)
            {
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                String sqlSELECT = "select ownerid, firstname + ' ' + middlename + ' ' + lastname as ownername from rs_tblowner where (firstname=@firstname or @firstname='') and (lastname=@lastname or @lastname='') and companyid=@companyid";

                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(sqlSELECT, myConnection);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@companyid";
                param.Value = companyid;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.NVarChar;
                param.ParameterName = "@lastname";
                param.Value = lastname;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.NVarChar;
                param.ParameterName = "@firstname";
                param.Value = firstname;
                myCommand.Parameters.Add(param);

                SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
                myConnection.Open();
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                gvOwners.DataSource = ds;
                gvOwners.DataBind();
                myConnection.Close();

                if (gvOwners.Rows.Count > 0)
                {
                    bHasValue = true;
                }
            }
            return bHasValue;
        }

        private DataSet GetChildListDataSet(int companyid, int ownerid)
        {
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            RSUserIdentity userIdentity = GetRSUserIdentity();
            String sqlSELECT = "select propertyid, street + ' ' + city + ' ' + state + ' ' + zip + ' ' + country as propertylocation, audit_createdt from rs_tblproperty where ownerid=@ownerid and companyid=@companyid";
 
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@ownerid";
            param.Value = ownerid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            return ds;
        }
        protected void gvOwners_OnRowDataBound(Object sender, GridViewRowEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView gv = (GridView)e.Row.FindControl("gvProperty");
                if (null != gv && gv is GridView)
                {
                    DataRowView drv = (DataRowView)e.Row.DataItem;
                    int ownerid = Convert.ToInt32(drv["OwnerID"]);
                    gv.DataSource = GetChildListDataSet(companyid, ownerid);
                    gv.DataBind();
                }
            }
        }
        protected void gvProperty_Add(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int ownerid = Convert.ToInt32(e.CommandArgument.ToString());
            if (ownerid > 0)
            {
                Response.Redirect("RSPropertyWithoutOwner.aspx?ownerid=" + ownerid);
            }
        }
        protected void gvProperty_Edit(object sender, CommandEventArgs e)
        {
            int propid = Convert.ToInt32(e.CommandArgument.ToString());
            if (propid > 0)
                Response.Redirect("RSPropertyDetail.aspx?propid=" + propid);
        }
        protected void gvProperty_Remove(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int propertyid = Convert.ToInt32(e.CommandArgument.ToString());
            if (propertyid > 0)
            {
                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyID", SqlDbType.Int, 0, ParameterDirection.Input, propertyid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Companyid", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
                SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "UPDATE rs_tblProperty SET OwnerID=NULL WHERE PropertyID=@PropertyID AND CompanyID=@CompanyID");
                SQLHelper.ExecuteScalarCmd(sqlCmd);
                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                LoadOwners(userIdentity.RSCompanyId);
            }
        }

    }
}